//
//  YHXUserModle.m
//  Yihaoqianbao
//  用户信息进行归档,转换二进制流后存储
//  Created by CPZX010 on 16/10/26.
//  Copyright © 2016年 Money. All rights reserved.
//

#import "YHXUserModle.h"
//#import "NSString+YHResponseDataAnalysis.h"
#import <objc/runtime.h>

#define FILEPATH    [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingString:@"/us.a"]


@implementation YHXUserModle


#pragma mark    updateInfor

- (void)yhxUpdateUserInforsWithUser:(YHXUserModle *)userModle {
    NSFileManager *maneger = [NSFileManager defaultManager];
    if ([maneger fileExistsAtPath:FILEPATH]) {
        [maneger removeItemAtPath:FILEPATH error:nil];
    }
    NSMutableData *data = [NSMutableData data];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:userModle forKey:@"user"];
    [archiver finishEncoding];
    BOOL success = [data writeToFile:FILEPATH atomically:YES];
    if (success) {
        NSLog(@"archiver Success");
    }else {
        NSLog(@"archiver Failed");
    }
}

+ (void)yhxDeleteUserInfors {
    BOOL success = [[NSFileManager defaultManager] removeItemAtPath:FILEPATH error:nil];
    if (success ) {
        NSLog(@"clean Success");
    }else {
        NSLog(@"clean Failed");
    }
}


#pragma mark      unArchiver  archiver
- (instancetype)initWithCoder:(NSCoder *)aDecoder  {
    self = [super init];
    if (self) {
        unsigned count = 0;
        Ivar *ivarList = class_copyIvarList([self class], &count);
        for (int i = 0; i < count; i ++) {
            Ivar ivar = ivarList[i];
            NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];
            NSString *key = [name substringFromIndex:1];
            id value = [aDecoder decodeObjectForKey:key];
            [self setValue:value forKey:key];
        }
        free(ivarList);
    }
    return self;
}


- (void)encodeWithCoder:(NSCoder *)aCoder {
    unsigned count = 0;
    Ivar *ivarList = class_copyIvarList([self class], &count);
    for (int i = 0 ; i < count; i ++) {
        Ivar ivar = ivarList[i];
        NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];
        NSString *key = [name substringFromIndex:1];
        id value  = [self valueForKey:key];
        [aCoder encodeObject:value forKey:key];
    }
    free(ivarList);
}

#pragma mark    creat   Modle



+ (YHXUserModle *)shareYHXUserModleWithInfors:(NSMutableDictionary *)infors{
    if ([[NSFileManager defaultManager] fileExistsAtPath:FILEPATH]) {
        NSMutableData *data = [[NSMutableData alloc] initWithContentsOfFile:FILEPATH];
        NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        YHXUserModle *modle = [unArchiver decodeObjectForKey:@"user"];
        return modle;
    }else {
        YHXUserModle *modle = [[self alloc] init];
        unsigned count = 0;
        Ivar *ivarList = class_copyIvarList([self class], &count);
#pragma mark    只进行二级封装, 不再进行三级封装
        for (int i = 0; i < count; i ++) {
            Ivar ivar = ivarList[i];
            NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];
            NSString *key =[name substringFromIndex:1];
            id value = [infors objectForKey:key];
            NSString *ivarType = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];
            if ([value isKindOfClass:[NSDictionary class]] && ![ivarType containsString:@"NS"]) {
                ivarType = [ivarType stringByReplacingOccurrencesOfString:@"@\"" withString:@""];
                ivarType = [ivarType stringByReplacingOccurrencesOfString:@"\"" withString:@""];
                Class modleClass = NSClassFromString(ivarType);
                value = [modleClass shareYHXUserModleWithInfors:value];
            }
        [modle setValue:value forKey:key];
        }
        free(ivarList);
        return modle;
    }
}




@end
